package com.huangyuan3h.StoreMarket.DAO;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.huangyuan3h.StoreMarket.model.Account;

@Repository
@Transactional
public class AccountDAOImpl implements AccountDAO {

	@Autowired
	private EntityManager em;

	@Override
	public Account FindByID(Long ID) {
		return em.find(Account.class, ID);
	}

	@Override
	public List<Account> findAllOrderedByName() {
		CriteriaBuilder cb = em.getCriteriaBuilder();
		CriteriaQuery<Account> criteria = cb.createQuery(Account.class);
		Root<Account> account = criteria.from(Account.class);
		criteria.select(account).orderBy(cb.asc(account.get("userName")));
		return em.createQuery(criteria).getResultList();
	}

	@Override
	public void register(Account account) {
		em.persist(account);
		return;
	}

	@Override
	public List<Account> FindByName(String UserName) {
		CriteriaBuilder cb = em.getCriteriaBuilder();
		CriteriaQuery<Account> criteria = cb.createQuery(Account.class);
		Root<Account> account = criteria.from(Account.class);
		Predicate condition = cb.equal(account.get("userName"), UserName);
		criteria.select(account).where(condition);
		return em.createQuery(criteria).getResultList();
	}

	@Override
	public List<Account> FindByEmail(String Email) {
		CriteriaBuilder cb = em.getCriteriaBuilder();
		CriteriaQuery<Account> criteria = cb.createQuery(Account.class);
		Root<Account> account = criteria.from(Account.class);
		Predicate condition = cb.equal(account.get("email"), Email);
		criteria.select(account).where(condition);
		return em.createQuery(criteria).getResultList();
	}

	@Override
	public List<Account> Login(String UserName, String Password) {
		CriteriaBuilder cb = em.getCriteriaBuilder();
		CriteriaQuery<Account> criteria = cb.createQuery(Account.class);
		Root<Account> account = criteria.from(Account.class);
		Predicate condition = cb.and(
				cb.equal(account.get("userName"), UserName),
				cb.equal(account.get("password"), Password));
		criteria.select(account).where(condition);
		return em.createQuery(criteria).getResultList();
	}

	@Override
	
	public void update(Account account) {
		Account dBAccount = em.find(Account.class, account.getId());
		//em.getTransaction().begin();		
		if(account.getUserName()!=null)dBAccount.setUserName(account.getUserName());
		if(account.getPassword()!=null)dBAccount.setPassword(account.getPassword());
		if(account.getAddress()!=null)dBAccount.setAddress(account.getAddress());
		if(account.getDob()!=null)dBAccount.setDob(account.getDob());
		if(account.getEmail()!=null)dBAccount.setEmail(account.getEmail());
		dBAccount.setMale(account.isMale());		
		if(account.getPhoneNumber()!=null)dBAccount.setPhoneNumber(account.getPhoneNumber());
		//em.getTransaction().commit();
	}

	@Override
	
	public void delete(Long ID) {
		Account dBAccount = em.find(Account.class, ID);
		//em.getTransaction().begin();
		em.remove(dBAccount);
		//em.getTransaction().commit();
	}

}
